[Elastic Beanstalk] AmazonLinux(2015.03)でEB CLIを試してみた
はじめに
AWSチームのすずきです。
AWSのElastic Beanstalk、コマンドラインで操作するツールとして EB CLIが提供されています。
Amazon Linux 2015.03環境でEB CLIをインストールして、新規アプリケーションを環境を作成、撤去するまでの一連の手順を紹介します。
参考
- Elastic Beanstalk 開発者ガイド: EB CLI
実行環境
EB CLIは、AWS SDK for Python (Boto) を利用するツールのため、OS、Pythonの実行環境の確認を行います。
OS
$ cat /etc/system-release Amazon Linux AMI release 2015.03
Python
$ python --version Python 2.7.9
EC2ロール
Elastic Beanstalkの実行にはIAM権限が必要です。 今回はEC2ロールとして、AdministratorのIAM権限を付与したインスタンスを利用しました。
$ curl http://169.254.169.254/latest/meta-data/iam/security-credentials/ ec2-Role-Admin
AWS CLI設定
EC2ロールを利用しない場合、アクセスキーを登録することも可能です。 ただしセキュリティ事故の原因となる事を避けるため、アクセスキーの利用は極力避ける事をお薦めします。
$ aws configure AWS Access Key ID [None]: AWS Secret Access Key [None]: Default region name [None]: ap-northeast-1 Default output format [None]:
インストール
pip, setuptools
最新バージョンの pip と、pipが必要とする setuptools のインストーラ(get-pip.py)を入手し、インストールを実施します。
$ curl -O https://bootstrap.pypa.io/get-pip.py % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1379k 100 1379k 0 0 4271k 0 --:--:-- --:--:-- --:--:-- 4284k
$ sudo python27 get-pip.py Collecting pip Downloading pip-7.1.1-py2.py3-none-any.whl (1.1MB) 100% |████████████████████████████████| 1.1MB 245kB/s Collecting wheel Downloading wheel-0.24.0-py2.py3-none-any.whl (63kB) 100% |████████████████████████████████| 65kB 3.5MB/s Installing collected packages: pip, wheel Found existing installation: pip 6.1.1 Uninstalling pip-6.1.1: Successfully uninstalled pip-6.1.1 Successfully installed pip-7.1.1 wheel-0.24.0
awsebcli
awsebcli(EB CLI)のインストールを実施します。
AmazonLinux(2015.03)ではpython27用のpipは「/usr/local/bin/pip」に配置される為、フルパスの指定が必要です。
$ which pip /usr/local/bin/pip $ sudo /usr/local/bin/pip install awsebcli Collecting awsebcli Downloading awsebcli-3.5.tar.gz (162kB) 100% |████████████████████████████████| 163kB 1.4MB/s Collecting pyyaml>=3.11 (from awsebcli) Downloading PyYAML-3.11.tar.gz (248kB) 100% |████████████████████████████████| 249kB 1.0MB/s Requirement already satisfied (use --upgrade to upgrade): botocore>=1.0.1 in /usr/lib/python2.7/dist-packages (from awsebcli) Collecting cement==2.4 (from awsebcli) Downloading cement-2.4.0.tar.gz (129kB) 100% |████████████████████████████████| 131kB 1.8MB/s Collecting colorama==0.3.3 (from awsebcli) Downloading colorama-0.3.3.tar.gz Collecting pathspec==0.3.3 (from awsebcli) Downloading pathspec-0.3.3.tar.gz Collecting docopt<0.7,>=0.6.1 (from awsebcli) Downloading docopt-0.6.2.tar.gz Collecting requests<2.7,>=2.6.1 (from awsebcli) Downloading requests-2.6.2-py2.py3-none-any.whl (470kB) 100% |████████████████████████████████| 471kB 575kB/s Collecting texttable<0.9,>=0.8.1 (from awsebcli) Downloading texttable-0.8.3.tar.gz Collecting websocket-client<1.0,>=0.11.0 (from awsebcli) Downloading websocket_client-0.32.0.tar.gz (192kB) 100% |████████████████████████████████| 192kB 1.3MB/s Collecting docker-py<1.2,>=1.1.0 (from awsebcli) Downloading docker-py-1.1.0.tar.gz (42kB) 100% |████████████████████████████████| 45kB 4.4MB/s Collecting dockerpty<0.4,>=0.3.2 (from awsebcli) Downloading dockerpty-0.3.4.tar.gz Collecting blessed==1.9.5 (from awsebcli) Downloading blessed-1.9.5-py2.py3-none-any.whl (77kB) 100% |████████████████████████████████| 77kB 2.8MB/s Requirement already satisfied (use --upgrade to upgrade): jmespath==0.7.1 in /usr/lib/python2.7/dist-packages (from botocore>=1.0.1->awsebcli) Requirement already satisfied (use --upgrade to upgrade): python-dateutil<3.0.0,>=2.1 in /usr/lib/python2.7/dist-packages (from botocore>=1.0.1->awsebcli) Requirement already satisfied (use --upgrade to upgrade): docutils>=0.10 in /usr/lib/python2.7/dist-packages (from botocore>=1.0.1->awsebcli) Requirement already satisfied (use --upgrade to upgrade): six in /usr/lib/python2.7/dist-packages (from websocket-client<1.0,>=0.11.0->awsebcli) Collecting wcwidth>=0.1.0 (from blessed==1.9.5->awsebcli) Downloading wcwidth-0.1.4-py2.py3-none-any.whl Building wheels for collected packages: awsebcli, pyyaml, cement, colorama, pathspec, docopt, texttable, websocket-client, docker-py, dockerpty Running setup.py bdist_wheel for awsebcli Stored in directory: /root/.cache/pip/wheels/19/d7/ee/e493a9dae4b1b1b52502befe49eae7a6d9522fbe9cce1aa2bf Running setup.py bdist_wheel for pyyaml Stored in directory: /root/.cache/pip/wheels/fa/db/f6/dee55793d344f1706dc4a5a693298f0115241d1085cc212364 Running setup.py bdist_wheel for cement Stored in directory: /root/.cache/pip/wheels/be/8e/c3/6974d615636ced07e30f79c5cb50326e4aa346144e50751794 Running setup.py bdist_wheel for colorama Stored in directory: /root/.cache/pip/wheels/e3/24/8d/aec3db961cfbc8e939dc1843126548e7d479349f96659067e9 Running setup.py bdist_wheel for pathspec Stored in directory: /root/.cache/pip/wheels/83/a3/f5/e4ccf270c4fec947942a8774b0dd60d4305421501e0b490151 Running setup.py bdist_wheel for docopt Stored in directory: /root/.cache/pip/wheels/0d/5c/a7/cb986749520c1950217b5d8405def5c18541322dbc411a80d1 Running setup.py bdist_wheel for texttable Stored in directory: /root/.cache/pip/wheels/d6/44/0b/20a20bc6ab19b4a7f4a43fa67010cf5008140d8abab61d58d5 Running setup.py bdist_wheel for websocket-client Stored in directory: /root/.cache/pip/wheels/48/45/ba/e955834950f99f1ca7a5778808d7bef1d4962edb1a4b14600a Running setup.py bdist_wheel for docker-py Stored in directory: /root/.cache/pip/wheels/ae/02/c0/3e415b95cc2f20db5c3089222952abe71d6f61c7b2b61eca78 Running setup.py bdist_wheel for dockerpty Stored in directory: /root/.cache/pip/wheels/52/29/66/0c53de7d30b0e2a838ba252f6db929e9cc3d528892e7d759d5 Successfully built awsebcli pyyaml cement colorama pathspec docopt texttable websocket-client docker-py dockerpty Installing collected packages: pyyaml, cement, colorama, pathspec, docopt, requests, texttable, websocket-client, docker-py, dockerpty, wcwidth, blessed, awsebcli Found existing installation: PyYAML 3.10 DEPRECATION: Uninstalling a distutils installed project (pyyaml) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project. Uninstalling PyYAML-3.10: Successfully uninstalled PyYAML-3.10 Found existing installation: colorama 0.2.5 DEPRECATION: Uninstalling a distutils installed project (colorama) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project. Uninstalling colorama-0.2.5: Successfully uninstalled colorama-0.2.5 Found existing installation: requests 1.2.3 Uninstalling requests-1.2.3: Successfully uninstalled requests-1.2.3 Successfully installed awsebcli-3.5 blessed-1.9.5 cement-2.4.0 colorama-0.3.3 docker-py-1.1.0 dockerpty-0.3.4 docopt-0.6.2 pathspec-0.3.3 pyyaml-3.11 requests-2.6.2 texttable-0.8.3 wcwidth-0.1.4 websocket-client-0.32.0
加筆(2015.8.23)
AmazonLinux(2015.03) にプリインストールされたpip(6.1.1)でも、ebcliのインストールは可能でした。 この場合「get-pip.py」のインストールは省略可能です。
$ sudo pip install ebcli You are using pip version 6.1.1, however version 7.1.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Collecting ebcli Downloading ebcli-3.0.2.tar.gz Collecting requires (from ebcli) Downloading Requires-0.0.3.tar.gz Collecting keyring (from ebcli) Downloading keyring-5.4.tar.gz (68kB) 100% |████████████████████████████████| 69kB 5.5MB/s Requirement already satisfied (use --upgrade to upgrade): requests in /usr/lib/python2.7/dist-packages (from ebcli) Installing collected packages: requires, keyring, ebcli Running setup.py install for requires Running setup.py install for keyring Running setup.py install for ebcli Successfully installed ebcli-3.0.2 keyring-5.4 requires-0.0.3
確認
EB CLIのインストールに成功すると「eb」コマンドが利用可能となります。 2015年8月現在、EB CLIのバージョンは3.5でした。
eb --version EB CLI 3.5 (Python 2.7.9)
テスト
テストコード設置
任意の作業用ディレクトリを作成し、PHPのテストコードを配置します。
$ mkdir -p ~/eb/helloword $ cd ~/eb/helloword $ cat << EOF > index.php <!--?php echo '</p> <p>Hello World</p> <p>'; ?--> EOF
アプリーケーションの設置
以下のアプリケーションを新規設置します。
- リージョン:ap-northeast-1 (東京)
- 新規アプリケーション:Create new Application
- アプリケーション名:helloword
- PHPバージョン:5.6
- SSH接続:実施する
- SSH鍵:EC2用の既存を指定
$ eb init Select a default region 1) us-east-1 : US East (N. Virginia) 2) us-west-1 : US West (N. California) 3) us-west-2 : US West (Oregon) 4) eu-west-1 : EU (Ireland) 5) eu-central-1 : EU (Frankfurt) 6) ap-southeast-1 : Asia Pacific (Singapore) 7) ap-southeast-2 : Asia Pacific (Sydney) 8) ap-northeast-1 : Asia Pacific (Tokyo) 9) sa-east-1 : South America (Sao Paulo) (default is 3): 8 Select an application to use 1) [ Create new Application ] (default is 2): 1 Enter Application Name (default is "helloword"): Application helloword has been created. It appears you are using PHP. Is this correct? (y/n): y Select a platform version. 1) PHP 5.6 2) PHP 5.5 3) PHP 5.4 4) PHP 5.3 (default is 1): 1 Do you want to set up SSH for your instances? (y/n): y Select a keypair. 1) xx-xxxxx 2) [ Create new KeyPair ] (default is 2): 1
環境作成
名称「dev-env」とした環境を作成します。
$ eb create dev-env Creating application version archive "app-150821_111235". Uploading helloword/app-150821_111235.zip to S3. This may take a while. Upload Complete. Environment details for: dev-env Application name: helloword Region: ap-northeast-1 Deployed Version: app-150821_111235 Environment ID: e-xxxxxxxxxx Platform: 64bit Amazon Linux 2015.03 v2.0.0 running PHP 5.6 Tier: WebServer-Standard CNAME: UNKNOWN Updated: 2015-08-21 11:12:38.421000+00:00 Printing Status: INFO: createEnvironment is starting. INFO: Using elasticbeanstalk-ap-northeast-1-784693731708 as Amazon S3 storage bucket for environment data. INFO: Environment health has transitioned to Pending. There are no instances. INFO: Created load balancer named: awseb-e-2-AWSEBLoa-14CZ8K6WCMY90 INFO: Created security group named: awseb-e-xxxxxxxxxx-stack-AWSEBSecurityGroup-1X1DPDH06QZOD INFO: Created Auto Scaling launch configuration named: awseb-e-xxxxxxxxxx-stack-AWSEBAutoScalingLaunchConfiguration-N23F97XNMH7M INFO: Waiting for EC2 instances to launch. This may take a few minutes. INFO: Created Auto Scaling group named: awseb-e-xxxxxxxxxx-stack-AWSEBAutoScalingGroup-VH9WJELJKZDO INFO: Added instance [i-057d18f7] to your environment. INFO: Environment health has transitioned from Pending to Ok. INFO: Successfully launched environment: dev-env
確認
ステータス
$ eb status Environment details for: dev-env Application name: helloword Region: ap-northeast-1 Deployed Version: app-150821_122406 Environment ID: e-9mx8h88q2i Platform: 64bit Amazon Linux 2015.03 v2.0.0 running PHP 5.6 Tier: WebServer-Standard CNAME: dev-env-xxxxxxxxxx.elasticbeanstalk.com Updated: 2015-08-21 12:29:03.800000+00:00 Status: Ready Health: Green
サンプルPHP表示
eb環境で作成されたELB(EIP)は、「eb status」→「CNAME」で表示される 「elasticbeanstalk.com」ドメインのFQDNでアクセスする事が可能です。
$ curl dev-env-xxxxxxxxxx.elasticbeanstalk.com Hello World
環境削除
- terminate を行うと、EC2、ELBが撤去されます。
$ eb terminate The environment "dev-env" and all associated instances will be terminated. To confirm, type the environment name: dev-env INFO: terminateEnvironment is starting. INFO: Deleted CloudWatch alarm named: awseb-e-xxxxxxxxxx-stack-AWSEBCloudwatchAlarmHigh-139RYJU7R5PBK INFO: Deleted CloudWatch alarm named: awseb-e-xxxxxxxxxx-stack-AWSEBCloudwatchAlarmLow-1A8PB5D6302A4 INFO: Deleted Auto Scaling group policy named: arn:aws:autoscaling:ap-northeast-1:784693731708:scalingPolicy:83729b99-db17-46ff-bac8-6e8895a2aca1:autoScalingGroupName/awseb-e-xxxxxxxxxx-stack-AWSEBAutoScalingGroup-VH9WJELJKZDO:policyName/awseb-e-xxxxxxxxxx-stack-AWSEBAutoScalingScaleUpPolicy-16UVZFPINHOMD INFO: Deleted Auto Scaling group policy named: arn:aws:autoscaling:ap-northeast-1:784693731708:scalingPolicy:35a332be-60ad-4dbb-a8f7-3f5a767cd72c:autoScalingGroupName/awseb-e-xxxxxxxxxx-stack-AWSEBAutoScalingGroup-VH9WJELJKZDO:policyName/awseb-e-xxxxxxxxxx-stack-AWSEBAutoScalingScaleDownPolicy-ZJZEEG21CHG0 INFO: Waiting for EC2 instances to terminate. This may take a few minutes. INFO: Removed instance [i-057d18f7] from your environment. WARN: Environment health has transitioned from Ok to Severe. ELB health is failing or not available for all instances. INFO: Deleted Auto Scaling group named: awseb-e-xxxxxxxxxx-stack-AWSEBAutoScalingGroup-VH9WJELJKZDO INFO: Deleted Auto Scaling launch configuration named: awseb-e-xxxxxxxxxx-stack-AWSEBAutoScalingLaunchConfiguration-N23F97XNMH7M INFO: Deleted security group named: awseb-e-xxxxxxxxxx-stack-AWSEBSecurityGroup-1X1DPDH06QZOD INFO: Deleted load balancer named: awseb-e-2-AWSEBLoa-14CZ8K6WCMY90 INFO: Environment health has transitioned from Severe to Ok. There are no instances. Auto Scaling group desired capacity is set to zero. INFO: Deleted security group named: sg-0865206d INFO: Deleting SNS topic for environment dev-env. INFO: terminateEnvironment completed successfully.
まとめ
この様にAmazonLinux、EB CLIの実行環境として簡単に利用する事が可能です。
AmazonLinuxを利用する事で、開発環境やSDKのバージョンに由来する不具合や、 改行コードなど環境に依存するトラブルを未然に避ける効果も期待出来ます。
Elastic Beanstalkの多くの操作、WebGUI(AWSコンソール)上で行う事が可能ですが、EB CLIも是非お試しください。